package View;

// The SlidePuzzleGUI class creates a panel which contains two subpanels.
// 1. In the north is a subpanel for controls (just a play button for now).
// 2. In the center a graphics
import Model.SlidePuzzleModel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// class SlidePuzzleGUI
// This class contains all the parts of the GUI interface
/**
 * 
 * @author Simon Marshall - jc209673
 */
public class SlidePuzzleGUI extends JPanel {

    /*
     * View class file for Slide Puzzle Assignment
     */
    private GraphicsPanel _puzzleGraphics;
    private SlidePuzzleModel _puzzleModel = new SlidePuzzleModel();
    //end instance variables
    JLabel timeLabel;
    JLabel moveLabel;
    JMenuBar gameMenu;
    boolean gameStarted = false;
    GameTimer gameTimer;
    MoveCounter moveCounter;
    JTextArea myStats;

    // constructor
    /**
     */
    public SlidePuzzleGUI() {

        // Create a button.  Add a listener to it.
        JLabel Name = new JLabel("Name: ");
        JTextField nameField = new JFormattedTextField("Enter Your Name ");
        moveLabel = new JLabel("Moves Done: 0");
        timeLabel = new JLabel("Time: 0");
        gameMenu = new JMenuBar();
        myStats = new JTextArea();

        //JButton newGameButton = new JButton("New Game");
        //newGameButton.addActionListener(new NewGameAction());

        // Create control panel
        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());
        //controlPanel.add(newGameButton);
        controlPanel.add(gameMenu);
        controlPanel.add(myStats);
        controlPanel.add(Name);
        controlPanel.add(nameField);
        controlPanel.add(moveLabel);
        controlPanel.add(timeLabel);

        // Create graphics panel
        _puzzleGraphics = new GraphicsPanel();

        // Set the layout and add the components
        this.setLayout(new BorderLayout());
        this.add(controlPanel, BorderLayout.NORTH);
        this.add(_puzzleGraphics, BorderLayout.CENTER);
        gameTimer = new GameTimer(this);
    }

    /**
     * 
     * @param counterz updates the time in second intervals in the top panel.
     */
    public void UpdateTimerLabel(int counterz) {
        timeLabel.setText("Time: " + counterz);
        //SlidePuzzleModel.isInFinalPosition = true;
        //gameTimer.doStop();
        //JOptionPane.showMessageDialog(gameMenu, "WOW!! you have won");
    }

    /**
     * 
     * @param counter updates the Moves Done in the top panel.
     */
    public void UpdateMoveLabel(int counter) {
        moveLabel.setText("Moves Done " + ++counter);
    }

    // This is defined inside the outer class so that it can use the outer class instance variables.
    class GraphicsPanel extends JPanel implements MouseListener {

        private static final int ROWS = 4;
        private static final int COLS = 4;
        private static final int CELL_SIZE = 85; // Pixels
        private Font _biggerFont;

        public GraphicsPanel() {
            _biggerFont = new Font("SansSerif", Font.BOLD, CELL_SIZE / 2);
            this.setPreferredSize(
                    new Dimension(CELL_SIZE * COLS, CELL_SIZE * ROWS));
            this.setBackground(Color.black);
            //this.addMouseListener(this);  // Listen own mouse events.
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (int r = 0; r < ROWS; r++) {
                for (int c = 0; c < COLS; c++) {
                    int x = c * CELL_SIZE;
                    int y = r * CELL_SIZE;
                    String text = _puzzleModel.getFace(r, c);
                    if (text != null) {
                        g.setColor(Color.gray);
                        g.fillRect(x + 2, y + 2, CELL_SIZE - 4, CELL_SIZE - 4);
                        g.setColor(Color.black);
                        g.setFont(_biggerFont);
                        g.drawString(text, x + 20, y + (3 * CELL_SIZE) / 4);
                    }
                }
            }
        }

        // listener mousePressed
        public void mousePressed(MouseEvent e) {
            if (gameStarted == false) {
                gameStarted = true;
                gameTimer.doStart();
            }

            //--- map x,y coordinates into a row and col.
            int col = e.getX() / CELL_SIZE;
            int row = e.getY() / CELL_SIZE;

            if (!_puzzleModel.moveTile(row, col)) {
                // moveTile moves tile if legal, else returns false.
                Toolkit.getDefaultToolkit().beep();
            }
            this.repaint();  // Show any updates to model.
        }

        // ignore these events
        public void mouseClicked(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }
    }

    // inner class NewGameAction
    /**
     */
    public class NewGameAction implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            gameTimer.doReset();
            gameStarted = false;
            _puzzleModel.reset();
            _puzzleGraphics.repaint();
        }
    }
}
